#include "machine_control.h"
#include "sensor_reader.h"
#include "timer_utils.h"
#include "main_motor.h"
#include <string.h>

// 模式处理函数声明
void standby_mode_enter(ModeConfig* config);
void standby_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state);
void standby_mode_exit(void);

void cooling_mode_enter(ModeConfig* config);
void cooling_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state);
void cooling_mode_exit(void);

void cleaning_mode_enter(ModeConfig* config);
void cleaning_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state);
void cleaning_mode_exit(void);

void defrost_mode_enter(ModeConfig* config);
void defrost_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state);
void defrost_mode_exit(void);

void preservation_mode_enter(ModeConfig* config);
void preservation_mode_process(ModeConfig* config, SensorData* sensors, DeviceState* state);
void preservation_mode_exit(void);

// 模式处理函数结构体
typedef struct {
    void (*enter)(ModeConfig* config);
    void (*process)(ModeConfig* config, SensorData* sensors, DeviceState* state);
    void (*exit)(void);
} ModeHandler;

// 模块内部状态
static struct {
    DeviceState state;
    SensorData sensors;
    ModeConfig config;
    MachineMode target_mode;
    bool mode_changing;
    uint32_t mode_start_time;
} s_machine;

// 模式处理函数表
static ModeHandler s_mode_handlers[MODE_MAX] = {
    [MODE_STANDBY] = {
        .enter = standby_mode_enter,
        .process = standby_mode_process,
        .exit = standby_mode_exit
    },
    [MODE_COOLING] = {
        .enter = cooling_mode_enter,
        .process = cooling_mode_process,
        .exit = cooling_mode_exit
    },
    [MODE_CLEANING] = {
        .enter = cleaning_mode_enter,
        .process = cleaning_mode_process,
        .exit = cleaning_mode_exit
    },
    [MODE_DEFROST] = {
        .enter = defrost_mode_enter,
        .process = defrost_mode_process,
        .exit = defrost_mode_exit
    },
    [MODE_PRESERVATION] = {
        .enter = preservation_mode_enter,
        .process = preservation_mode_process,
        .exit = preservation_mode_exit
    }
};

void machine_init(void) {
    memset(&s_machine, 0, sizeof(s_machine));
    
    // 初始化默认配置
    s_machine.config = (ModeConfig){
        .cooling_target = -3.0f,
        .cooling_hyst = 1.0f,
        .cooling_min_current = 200,
        .cooling_max_current = 800,
        .preservation_target = 4.0f,
        .preservation_hyst = 1.0f,
        .cleaning_phase1_time = 300,   // 5分钟
        .cleaning_phase2_time = 600,   // 10分钟
        .cleaning_phase3_time = 300,   // 5分钟
        .cleaning_target_temp = 60.0f,
        .defrost_target = 5.0f,
        .defrost_timeout = 1800        // 30分钟
    };
    
    // 初始状态
    s_machine.state.current_mode = MODE_STANDBY;
    s_machine.target_mode = MODE_STANDBY;
    s_machine.state.in_error = false;
    s_machine.state.error_code = ERROR_NONE;
    
    // 初始化硬件
    sensor_init();
    actuator_init();
    
    // 进入初始模式
    if (s_mode_handlers[MODE_STANDBY].enter) {
        s_mode_handlers[MODE_STANDBY].enter(&s_machine.config);
    }
    
    s_machine.mode_start_time = get_system_time_sec();
}

bool machine_set_mode(MachineMode mode) {
    if (mode >= MODE_MAX || s_machine.mode_changing) {
        return false;
    }
    
    // 运行模式切换需先进入待机
    if (s_machine.state.current_mode != MODE_STANDBY && mode != MODE_STANDBY) {
        s_machine.target_mode = MODE_STANDBY;
        s_machine.mode_changing = true;
        return true;
    }
    
    s_machine.target_mode = mode;
    s_machine.mode_changing = true;
    return true;
}

MachineMode machine_get_current_mode(void) {
    return s_machine.state.current_mode;
}

void machine_run_handler(void) {
    // 读取传感器数据
    sensor_read(&s_machine.sensors);
    
    // 检查传感器错误
    if (!s_machine.sensors.temp_sensor_ok) {
        s_machine.state.in_error = true;
        s_machine.state.error_code = ERROR_TEMP_SENSOR;
    } else if (!s_machine.sensors.current_sensor_ok) {
        s_machine.state.in_error = true;
        s_machine.state.error_code = ERROR_CURRENT_SENSOR;
    }
    
    // 错误处理
    if (s_machine.state.in_error) {
        // 紧急停机
        for (int i = 0; i < MODE_MAX; i++) {
            if (s_mode_handlers[i].exit) {
                s_mode_handlers[i].exit();
            }
        }
        return;
    }
    
    // 模式切换处理
    if (s_machine.mode_changing) {
        // 退出当前模式
        if (s_machine.state.current_mode < MODE_MAX && 
            s_mode_handlers[s_machine.state.current_mode].exit) {
            s_mode_handlers[s_machine.state.current_mode].exit();
        }
        
        // 进入新模式
        s_machine.state.current_mode = s_machine.target_mode;
        if (s_machine.state.current_mode < MODE_MAX && 
            s_mode_handlers[s_machine.state.current_mode].enter) {
            s_mode_handlers[s_machine.state.current_mode].enter(&s_machine.config);
        }
        
        s_machine.mode_start_time = get_system_time_sec();
        s_machine.mode_changing = false;
    }
    
    // 更新运行时间
    s_machine.state.mode_runtime = get_system_time_sec() - s_machine.mode_start_time;
    
    // 执行当前模式处理
    if (s_machine.state.current_mode < MODE_MAX && 
        s_mode_handlers[s_machine.state.current_mode].process) {
        s_mode_handlers[s_machine.state.current_mode].process(
            &s_machine.config, &s_machine.sensors, &s_machine.state);
    }
}

const DeviceState* machine_get_state(void) {
    return &s_machine.state;
}

const SensorData* machine_get_sensors(void) {
    return &s_machine.sensors;
}

void machine_set_config(const ModeConfig* config) {
    if (config != NULL) {
        s_machine.config = *config;
    }
}

const ModeConfig* machine_get_config(void) {
    return &s_machine.config;
}

void machine_clear_error(void) {
    s_machine.state.in_error = false;
    s_machine.state.error_code = ERROR_NONE;
    motor_reset_protection();
}
